function debounce(fn, wait = 500) {
  let timer = null;
  return function () {
    if (timer) clearTimeout(timer)
    timer = setTimeout(() => {
      timer = null;
      fn.apply(this, arguments)
    }, wait)
  }
}


document.getElementById('input').addEventListener('input', debounce((e) => {
  console.log("change", e.target.value, new Date - 0)
}))


// 节流
// 定时器方案
function throttle(fn, wait=1000) {
  let timer = null;
  return function () {
    if (!timer) {
      timer = setTimeout(() => {
        fn.apply(this, arguments);
        timer = null;
      }, wait)
    }
  }
}
// 时间戳方案
function timeThrottle(fn,wait=1000){
  let start=new Date().getTime();
  return function (){
    let end = new Date().getTime()
    if(end > start+wait){
      start=new Date().getTime()
      fn.apply(this,arguments)
    }
  }
}
window.addEventListener("mousemove",timeThrottle((e)=>{
  console.log(e)
},1000));
